package com.pesilo.mvc.view.component.show.watch
{
	import com.greensock.TweenMax;
	import com.greensock.events.LoaderEvent;
	import com.greensock.loading.ImageLoader;
	import com.greensock.loading.LoaderMax;
	import com.pesilo.base.BaseComponent;
	import com.pesilo.event.PesiloEvent;
	import com.pesilo.mvc.model.vo.WatchVO;
	import com.pesilo.util.Logger;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;

	public class WatchComponent extends BaseComponent
	{
		private const page:int=15;//每页的个数
		private var watchCount:int=5;//显示出的个数
		
		private var pageIndex:int=0;//当前指向第几页
		private var vo:WatchVO;
		private var len:int;
		private var mc:Sprite;
		
		private var rulerCmp:RulerComponent;
		
		private var queue:LoaderMax;
		private var contArr:Array;//源图片数组
		private var rulerArr:Array;
		private var watchArr:Array;//图片Cmp数组
		private var imgUrlArr:Array;
		
		private var watchMc:Sprite;
		
		private var index:int=0;
		
		public function WatchComponent()
		{
			super();
		}
		
		override public function initUI():void{
			Logger.logInfo("init WatchComponent");
			
			mc=new Sprite();
			this.addChild(mc);

			watchMc=new Sprite();
			mc.addChild(watchMc);
			
			rulerCmp=new RulerComponent();
			mc.addChild(rulerCmp);
			
			queue=new LoaderMax({
				name:"mainQueue",
				onProgress:__queueProgressHandler,
				onComplete:__queueCompleteHandler, 
				onError:__queueErrorHandler	
			});
			
		}
		
		public function photo(vo:WatchVO):void{
			if(!mc){
				Logger.logInfo("WatchComponent is not addChild");
				return;
			}
			Logger.logInfo("photo data!");
			this.vo=vo;
			len=(vo.list.length-page*pageIndex)>page?page:(vo.list.length-page*pageIndex);
			
			queue.unload();
			imgUrlArr=[];
			for(var i:uint=page*pageIndex;i<len+page*pageIndex;i++){
				queue.prepend(new ImageLoader(vo.list[i].thumb,{name:"thumb_"+i}));
				imgUrlArr.push(vo.list[i].img);
			}
			queue.load();
		}
		
		//图片加载
		private function __queueProgressHandler(e:LoaderEvent):void{
			rulerCmp.loadingProgressInfo(e.target.progress,len);
			initLocation();
		}
		
		//图片加载完成
		private function __queueCompleteHandler(e:LoaderEvent):void{
			contArr=[];
			for(var i:uint=page*pageIndex;i<len+page*pageIndex;i++){
				contArr.push(queue.getContent("thumb_"+i));
			}
			createWatchPicList();
			
			rulerArr=[];
			for(var j:uint=0;j<contArr.length;j++){
				var bitmapData:BitmapData=new BitmapData(70,70);
				var matrix:Matrix=new Matrix();
				var sy:Number=70*1/contArr[j].height;
				var sx:Number=contArr[j].width*sy/contArr[j].width;
				matrix.scale(sx,sy);
				var tx:Number=-(contArr[j].width*sy/2-70/2);
				matrix.translate(tx,0);
				bitmapData.draw(contArr[j],matrix);
				
				rulerArr.push(bitmapData);
			}
			rulerCmp.createRulerList(rulerArr);
			
			rulerCmp.rollOverEff();
		}
		
		/**
		 * 从现在 的图片 过度到目标图片
		 */
		public function changeIndex(change:int):void{
			var toRight:int=change-index<0?change-index+len:change-index;
			var toLeft:int= index-change<0?index-change+len:index-change;
			var short:int=Math.min(toRight,toLeft);
			var flag:int=toRight>toLeft?-1:1;	
			var tag:int=index;
			for(var i:int=1;i<=short;i++){
				TweenMax.delayedCall(i*0.2,watchIndex,[tag+flag*i]);
			}
			TweenMax.delayedCall(short*0.2,setIndex,[change]);
			
		}
		
		private function setIndex(change:int):void{
			index=change;
		}
		//创建图
		private function createWatchPicList():void
		{
			createWatchCount();
			
			watchIndex(index);
		}
		
		/**
		 * 创建watchCmp中 图片的UI。 visible为false 透明度为0
		 */
		private function createWatchCount():void
		{
			while(watchMc.numChildren)watchMc.removeChildAt(0);
			watchArr=[];
			for(var i:uint=0;i<len;i++){
				var watchItemCmp:WatchItemComponent=new WatchItemComponent(i,contArr[i],imgUrlArr[i]);
				watchMc.addChild(watchItemCmp);
				watchArr.push(watchItemCmp);
				
				watchItemCmp.alpha=0;
				watchItemCmp.visible=false;
				
				watchItemCmp.addEventListener(PesiloEvent.ITEM_CLICK_EVENT,__watchItemClickHandle);
			}
		}
		
		protected function __watchItemClickHandle(e:PesiloEvent):void
		{
			changeIndex(int(e.data));
			rulerCmp.rulerArr[int(e.data)].showSign(true);
		}
		
		/**
		 * 根据指定的index值 呈现出来
		 */
		private function watchIndex(watchIndex:int=0):void
		{
			watchCount=5;
			if(len<=5&&len>3)watchCount=3;
			if(len<=3)watchCount=1;
			
			var listIndexArr:Array=getListIndexArr(watchIndex,len);
			var listFirstArr:Array=getListFirstArr(listIndexArr);
			
			var j:int=0;
			var flag:int=1;
			for(var i:int=0;i<listFirstArr.length;i++){
				flag*=-1;
				j=(i*flag+j);
				var num:int=0+j;// 0  1  -1  2  -2 这样的规律
				
				var w:int=listFirstArr[i];
				watchArr[w].visible=true;
				if(i==0){
					TweenMax.to(watchArr[w],0.5,{x:num*150,alpha:1,rotationY:num*15,z:Math.abs(num)*170,onComplete:watchTweenCompleteHandle,onCompleteParams:[watchArr[w]]});
				}else{
					TweenMax.to(watchArr[w],0.5,{x:num*150,alpha:1,rotationY:num*15,z:Math.abs(num)*170});
				}
				
				watchMc.setChildIndex(watchArr[w],watchMc.numChildren-1-i);
			}
			for(var l:int=0;l<len-watchCount;l++){
				TweenMax.to(watchMc.getChildAt(l),0.5,{alpha:0,x:0});
			}
		}
		
		private function watchTweenCompleteHandle(watchItem:WatchItemComponent):void{
			watchItem.current();
		}
		
		/**
		 * @arr 需要排列的数组
		 * 将一个数组从中间到两边扩散的组成一个新的数组
		 */
		private function getListFirstArr(arr:Array):Array{
			var firstArr:Array=[];
			
			var j:int=0;
			var flag:int=1;
			for(var i:int=0;i<arr.length;i++){
				flag*=-1;
				j=(i*flag+j);
				var num:int=int(arr.length/2)+j;
				
				firstArr.push(arr[num]);
			}
			return firstArr;
		}
		
		/**
		 * @index 当前状态的图片索引
		 * @len 本轮所有图片的个数
		 * 
		 * 给定一个数字，取出这个数左右的数字 环形    数组
		 */
		private function getListIndexArr(index:int,len:int):Array
		{
			var listIndexArr:Array=[];
			var li:int=Math.floor(watchCount/2);
			for(var i:int=li*-1;i<li+1;i++){
				var num:int;
				if(i+index<0){
					num=len+(i+index);
				}else if(i+index>=len){
					num=(i+index)%len;
				}else{
					num=i+index;
				}
				listIndexArr.push(num);
			}
			return listIndexArr;
		}
		
		protected function __rulerItemClickHandle(e:PesiloEvent):void
		{
			var change:int=int(e.data);
			changeIndex(change);
		}	
		
		//图片加载错误
		private function __queueErrorHandler(e:LoaderEvent):void{
			
		}
		
		override public function initEvent(b:Boolean=true):void{
			if(b){
				rulerCmp.addEventListener(PesiloEvent.ITEM_CLICK_EVENT,__rulerItemClickHandle);
			}else{
				rulerCmp.removeEventListener(PesiloEvent.ITEM_CLICK_EVENT,__rulerItemClickHandle);
			}
		}
		
			

		
		override public function initLocation():void{
			root.transform.perspectiveProjection.projectionCenter = new 
			Point(stage.stageWidth/2,stage.stageHeight/2-30);
			
			rulerCmp.x=stage.stageWidth/2-rulerCmp.width/2;
			rulerCmp.y=stage.stageHeight-30;;
			
			watchMc.x=stage.stageWidth/2;
			watchMc.y=stage.stageHeight/2-30;
		}
	}
}